"use client";
import request from "@/app/(dashboard)/common/request";
import { IntegrityCheckReq, IntegrityCheckRes, IsUploadedReq, IsUploadedRes, SliceMergeReq, SliceMergeRes } from "@/types/api/file/sliceUpload";
import { SliceType } from "./sliceUpload";
import { AxiosProgressEvent } from "axios";

/**分片 - 是否上传 */
export async function sliceIsUploadReq(md5: string, suffix: string, targetPath: string) {
    return await request.post<IsUploadedRes, IsUploadedReq>('/file/sliceUpload/isUploaded', { md5, suffix, path: targetPath });
}
/**分片 - 上传 */
export function sliceUploadReq(formData: FormData) {
    return request.postByForm('/file/sliceUpload/upload', formData, { noCancel: true, showTip: false });
}
/**分片 - 完整性校验 */
export async function sliceIntegrityCheckReq(sliceList: SliceType[], md5: string) {
    return await request.post<IntegrityCheckRes, IntegrityCheckReq>('/file/sliceUpload/integrityCheck', { count: sliceList.length, md5 });
}
/**分片 - 合并 */
export async function sliceMergeReq(md5: string, suffix: string, targetPath: string, ownFileName?: string) {
    if (ownFileName) {
        return await request.post<SliceMergeRes, SliceMergeReq>('/file/sliceUpload/merge', { md5, suffix, path: targetPath, ownFileName });
    }
    return await request.post<SliceMergeRes, SliceMergeReq>('/file/sliceUpload/merge', { md5, suffix, path: targetPath });
}
/**普通上传 */
export async function commonUploadReq(url: string, formData: FormData, onUploadProgress: ((progressEvent: AxiosProgressEvent) => void) | undefined) {
    return await request.postByForm(url, formData, { onUploadProgress });
}
